<?php
/**
 * 《客户来访》实现类
 *
 * @author 孙晓晔
 * @version $Id: Visit.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/CustomerUtil.class.php');

class Visit extends CustomerUtil {
	public $tbl = Constant::tbl_visit;

	public $urgent = array(
		'-' => '请选择',
		'1' => '普通',
		'2' => '重要',
		'3' => '紧急',
		
		'9' => '其它',
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '预计来访',
		'2' => '等待来访',
		'3' => '正在访问',
		'4' => '完成访问',
	
		'8' => '取消来访',
		
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'emp' => '业务员',
		'customer' => '客户',
		't1' => '开始日期',
		't2' => '完成日期',
	
		
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	var $continent;

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('urgent_option', $this->urgent);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'customer/visit/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
		
		if ($this->_audit('164')) {
			return ' visible = 1 ';
		}

		$emp = $_SESSION['id'];
		
		return sprintf(" visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}

	function _where($request) {
		
		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row['c'] = $this->id(Constant::tbl_customer, $row['customer']);
				$row['country_t'] = Name::country($row['c']['country']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'customer/visit/list');

		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);
		
		$controller->assign('t1', strtotime('1 day'));
		$controller->assign('t2', strtotime('2 day'));

		$controller->display($request, 'customer/visit/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$customer = $this->_input($request['customer']);
		$customer_x = addslashes($request['customer_x']);
		
		$name = addslashes($request['name']);
		$content = addslashes($request['content']);

		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$day = $request['day'];

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(customer, customer_x, name, content, t1, t2, day, emp, oper, createtime, modifytime)";
		$fmt .=" VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $customer, $customer_x, $name, $content, $t1, $t2, $day, $emp, $_SESSION['id']);
		debug($sql);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'customer/visit/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$customer = $this->_input($request['customer']);
		$customer_x = addslashes($request['customer_x']);
		
		$name = addslashes($request['name']);
		$content = addslashes($request['content']);

		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$day = $request['day'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET customer = '%s', customer_x = '%s', name = '%s', content = '%s', t1 = '%s', t2 = '%s'";
		$fmt .= ", day = '%s', emp = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $customer, $customer_x, $name, $content, $t1, $t2, $day, $emp, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'customer/visit/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);		

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'customer/visit/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['customer_t'] = Name::customer_code($row['customer']);
		$row['customer_x'] = json_decode($row['customer_x']);
		
		$row['t3'] = $this->_t3($row['t2']);
		
		$row = $this->_view_t($row);
		
		return $row;
	}
	
	function _t3($t2) {		
		if (strtotime($t2) < time()) {
			return 0;
		}
		
		return $this->date_diff('d', date("Y-m-d"), $t2);
	}
}
?>
